<!doctype html>
<html lang="zh-Hans">
	<head>
		<title>电容代码计算器 - 嵌入式工具集</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<!-- 引入Tailwind CSS -->
		<script src="https://cdn.tailwindcss.com"></script>
		<!-- 以下为浏览器中的配置，正式 编译之后请注释掉这段-->
		<link rel="stylesheet" href="../styles/styles.css" />
		<!-- 以下为LCEDA中的配置，正式 编译之后请勿注释这段-->
		<link rel="stylesheet" href="/iframe/styles/styles.css" />
		<style>
			/* 电容代码计算器特定样式覆盖 */
			.capacitor-code-calculator .input-label {
				min-width: 100px;
			}

			/* 图标样式 - 确保在嘉立创EDA环境中正常显示 */
			.fa {
				display: inline-block;
				width: 1em;
				height: 1em;
				font-size: inherit;
				line-height: 1;
				vertical-align: middle;
				text-align: center;
			}

			.fa-exchange:before {
				content: '⇄';
				font-size: 1.2em;
			}

			.fa-refresh:before {
				content: '↺';
				font-size: 1.2em;
			}

			/* 增强按钮样式 - 与电容换算器保持一致 */
			.btn-primary {
				background-color: #0891b2;
				color: white;
				border: none;
				border-radius: 8px;
				padding: 0.75rem;
				cursor: pointer;
				transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
				font-weight: 600;
				font-size: 1rem;
				display: flex;
				align-items: center;
				justify-content: center;
				box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
				height: 3.5rem;
			}

			.btn-primary:hover:not(.bg-gray-400) {
				background-color: #0e7490;
				transform: translateY(-2px);
				box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
			}

			.btn-secondary {
				background-color: #f59e0b;
				color: white;
				border: none;
				border-radius: 8px;
				padding: 0.75rem;
				cursor: pointer;
				transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
				font-weight: 600;
				font-size: 1rem;
				display: flex;
				align-items: center;
				justify-content: center;
				box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
				height: 3.5rem;
			}

			.btn-secondary:hover {
				background-color: #d97706;
				transform: translateY(-2px);
				box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
			}
		</style>
	</head>
	<body class="bg-gradient-to-br from-light to-blue-50 min-h-screen p-6 font-sans">
		<div class="w-full mx-auto bg-white rounded-2xl shadow-lg p-6 calculator-container capacitor-code-calculator">
			<h2 class="text-2xl font-bold text-primary mb-6 text-center">电容代码计算器</h2>

			<div class="formula-box">
				<div class="formula-title">电容代码转换原理</div>
				<div class="formula-text">3位/4位代码 → 实际电容值 (pF)</div>
			</div>

			<!-- 输入区域 -->
			<div class="input-group">
				<div class="input-label">电容代码</div>
				<div class="input-field">
					<input
						type="text"
						id="capacitor-code"
						class="input-live"
						placeholder="请输入3位或4位代码，如104"
						maxlength="4"
						pattern="[0-9]+"
					/>
				</div>
			</div>

			<div class="input-group">
				<div class="input-label">误差代码</div>
				<div class="input-field">
					<select id="tolerance-code" class="input-live">
						<option value="">不指定误差</option>
						<option value="B">B (±0.1pF)</option>
						<option value="C">C (±0.25pF)</option>
						<option value="D">D (±0.5pF)</option>
						<option value="F">F (±1%)</option>
						<option value="G">G (±2%)</option>
						<option value="J">J (±5%)</option>
						<option value="K">K (±10%)</option>
						<option value="M">M (±20%)</option>
						<option value="Z">Z (+80%, -20%)</option>
					</select>
				</div>
			</div>

			<!-- 操作区域 -->
			<div class="flex items-center space-x-3 mt-2">
				<!-- 转换按钮 -->
				<button onclick="capacitorCodeCalculate()" id="convert-button" class="btn-primary flex-1">
					<i class="fa fa-exchange mr-2"></i>转换
				</button>

				<!-- 清空按钮 -->
				<button onclick="capacitorCodeClear()" class="btn-secondary flex-1"><i class="fa fa-refresh mr-2"></i>重置</button>
			</div>

			<!-- 结果显示区域 -->
			<div id="capacitor-results" class="result-cards">
				<div class="result-card picofarad-card">
					<div class="result-title">皮法 (pF)</div>
					<div class="result-value" id="result-pF">--</div>
				</div>
				<div class="result-card nanofarad-card">
					<div class="result-title">纳法 (nF)</div>
					<div class="result-value" id="result-nF">--</div>
				</div>
				<div class="result-card microfarad-card">
					<div class="result-title">微法 (μF)</div>
					<div class="result-value" id="result-μF">--</div>
				</div>
				<div class="result-card tolerance-card">
					<div class="result-title">误差范围</div>
					<div class="result-value" id="result-tolerance">--</div>
				</div>
			</div>

			<!-- 说明区域 -->
			<div class="info-section">
				<div class="info-title">电容代码转换规则</div>
				<ul class="info-list">
					<li><i class="fa fa-info-circle text-primary mr-1"></i>3位代码：前两位为有效数字，第三位为10的幂次方，结果单位为pF</li>
					<li><i class="fa fa-info-circle text-primary mr-1"></i>4位代码：前三位为有效数字，第四位为10的幂次方，结果单位为pF</li>
					<li><i class="fa fa-info-circle text-primary mr-1"></i>例如：104 = 10 × 10⁴ pF = 100000 pF = 100 nF = 0.1 μF</li>
				</ul>
			</div>
		</div>

		<script>
			// 更新结果并添加动画效果
			function updateResultWithAnimation(elementId, value) {
				const element = document.getElementById(elementId);
				if (!element) return;

				// 添加过渡效果
				element.style.transition = 'all 0.3s ease-in-out';
				element.style.opacity = '0.5';
				element.style.transform = 'scale(0.95)';

				setTimeout(() => {
					element.textContent = value;
					element.style.opacity = '1';
					element.style.transform = 'scale(1)';
				}, 150);
			}

			// 电容代码转换计算
			function capacitorCodeCalculate() {
				const codeInput = document.getElementById('capacitor-code').value.trim();
				const toleranceCode = document.getElementById('tolerance-code').value;

				// 验证输入
				if (!codeInput || !/^\d+$/.test(codeInput)) {
					alert('请输入有效的数字代码');
					return;
				}

				let capacitance = 0;

				// 根据代码位数进行转换
				if (codeInput.length === 3) {
					// 3位代码：前两位为有效数字，第三位为10的幂次方
					const significantDigits = parseInt(codeInput.substring(0, 2));
					const exponent = parseInt(codeInput.substring(2, 3));
					capacitance = significantDigits * Math.pow(10, exponent);
				} else if (codeInput.length === 4) {
					// 4位代码：前三位为有效数字，第四位为10的幂次方
					const significantDigits = parseInt(codeInput.substring(0, 3));
					const exponent = parseInt(codeInput.substring(3, 4));
					capacitance = significantDigits * Math.pow(10, exponent);
				} else {
					alert('请输入3位或4位数字代码');
					return;
				}

				// 转换为其他单位
				const capacitanceInNF = capacitance / 1000;
				const capacitanceInUF = capacitance / 1000000;

				// 获取误差信息
				const toleranceInfo = getToleranceInfo(toleranceCode);

				// 更新显示，添加动画效果
				updateResultWithAnimation('result-pF', formatCapacitance(capacitance) + ' pF');
				updateResultWithAnimation('result-nF', formatCapacitance(capacitanceInNF) + ' nF');
				updateResultWithAnimation('result-μF', formatCapacitance(capacitanceInUF) + ' μF');
				updateResultWithAnimation('result-tolerance', toleranceInfo);
			}

			// 获取误差信息
			function getToleranceInfo(code) {
				const toleranceMap = {
					'B': '±0.1 pF',
					'C': '±0.25 pF',
					'D': '±0.5 pF',
					'F': '±1%',
					'G': '±2%',
					'J': '±5%',
					'K': '±10%',
					'M': '±20%',
					'Z': '+80%, -20%',
				};
				return toleranceMap[code] || '不指定';
			}

			// 格式化电容值
			function formatCapacitance(value) {
				if (Math.abs(value) >= 1000000) {
					return (value / 1000000).toFixed(6).replace(/\.?0+$/, '') + 'M';
				} else if (Math.abs(value) >= 1000) {
					return (value / 1000).toFixed(6).replace(/\.?0+$/, '') + 'k';
				} else if (Math.abs(value) < 0.001) {
					return value.toExponential(3);
				} else if (Number.isInteger(value)) {
					return value.toString();
				} else {
					return value.toFixed(6).replace(/\.?0+$/, '');
				}
			}

			// 清空输入和结果
			function capacitorCodeClear() {
				document.getElementById('capacitor-code').value = '';
				document.getElementById('tolerance-code').value = '';
				// 使用动画效果清空结果
				updateResultWithAnimation('result-pF', '--');
				updateResultWithAnimation('result-nF', '--');
				updateResultWithAnimation('result-μF', '--');
				updateResultWithAnimation('result-tolerance', '--');
			}

			// 页面加载时初始化
			document.addEventListener('DOMContentLoaded', function () {
				const codeInput = document.getElementById('capacitor-code');
				const toleranceSelect = document.getElementById('tolerance-code');

				// 自动将输入转换为大写（如果输入字母）
				codeInput.addEventListener('input', function (e) {
					// 只允许数字输入
					this.value = this.value.replace(/[^0-9]/g, '');
				});

				// 支持回车键触发计算
				codeInput.addEventListener('keyup', function (event) {
					if (event.key === 'Enter') {
						capacitorCodeCalculate();
					}
				});
			});

			// 暴露给父页面的接口
			window.capacitorCodeCalculate = capacitorCodeCalculate;
			window.capacitorCodeClear = capacitorCodeClear;
		</script>

		<!-- 页脚 -->
		<footer class="w-full mx-auto mt-6 text-center text-sm text-gray-500">
			<p>版本号: V0.0.1-20251004</p>
		</footer>
	</body>
</html>
